<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class PaymentAtHome_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_ticket';
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}

        public function updateTransportTicket($data,$where=NULL){
                if($where){
                    foreach($where as $keys => $vals){
                        $this->db->where($keys, $vals);
                    }
                }
                return $this->db->update($this->table_name, $data); 
        }
        
        public function getInfoAjax(){
                $this->db->select('count(*) AS count');
                $this->db->from($this->table_name);
                $this->db->where('payment_confirm','0');
                $query = $this->db->get();
		return $query->result();
        }
        
        public function init_data($right){
		return array(
                                array(
						'name' => 'booking_id',
						'label' => $this->lang->line('booking_id'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),array(
						'name' => 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'sale_fare',
						'label' => $this->lang->line('net_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'sale_fare',
						'label' => $this->lang->line('sale_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'info',
						'label' => $this->lang->line('info'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'ticketWorkers',
						'label' => $this->lang->line('ticketWorkers'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'district',
						'label' => $this->lang->line('district'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'payment_company',
						'label' => $this->lang->line('payment_company'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'note',
						'label' => $this->lang->line('note'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'createdTime',
						'label' => $this->lang->line('createdTime'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label'  => '',
						'searchoptions' => FALSE
				)
		);
	}
	
	public function json_data($controller, $right){
                if($this->session->userdata('p_search')){
                    $params = json_decode($this->session->userdata('p_search'));
                }
            
		$this->datatables
		->select("bkt.id,bkt.booking_id,bkt.customerID,bkt.phone,bkt.address,bkt.province,bkt.payment_confirm,bkt.amount,bkt.note AS noteTK,bkt.lastupdated,bp.name,wk.name,dist.name AS district_name,usr.username,blb.booking_place_code,blb.sale_fare,blb.net_fare,blb.note AS noteBK,blb.createdTime AS ngaydat,bc.name AS khachhang")
		->from($this->table_name.' AS bkt')
                ->join('book_province AS bp','bp.id = bkt.province')
                ->join('book_agent_code AS agent','agent.id = bkt.agent_code')
                ->join('book_ticketworkers AS wk','wk.id = bkt.ticketWorkers','LEFT')
                ->join('book_district AS dist','dist.id = bkt.district','LEFT')
                ->join('users AS usr','usr.id = bkt.owner')
                ->join('book_list_booking AS blb','blb.id = bkt.booking_id','RIGHT')
                ->join('book_contacts AS bc','bc.customerID = bkt.customerID')
                ->where('bkt.payment_confirm = 0');
                
                if(isset($params) && $params->ticketWorkers != ''){
                    $this->datatables->where('bkt.ticketWorkers',$params->ticketWorkers);
                }
                if(isset($params) && $params->province != ''){
                    $this->datatables->where('bkt.province',$params->province);
                }
                if(isset($params) && $params->district != ''){
                    $this->datatables->where('bkt.district',$params->district);
                }
                if(isset($params) && $params->status != ''){
                    $this->datatables->where('bkt.payment_confirm',$params->status);
                }
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
                $this->session->unset_userdata('p_search');

		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
//                                        $item['id'],
                                        $item['booking_id'],
					$item['khachhang'],
                            		$item['booking_place_code'],
                                        $this->bookinglib->my_number_format($item['net_fare'],2, ',', ','),
                                        $this->bookinglib->my_number_format($item['sale_fare'],2, ',', ','),
                                        $item['phone'].'<br/>'.$item['address'],
                            		$item['name'],
                            		$item['district_name'],
                                        ($item['payment_confirm'] == 1)?'Đã thu tiền':'Chưa thu tiền',
                                        ($item['noteBK'])?$item['noteBK']:$item['noteTK'],
                                        $item['username'],
                                        date("d-m-Y H:i:s",$item['ngaydat']),
                                        date("d-m-Y H:i:s",$item['lastupdated']),
					$this->add_button_payment($controller, $right, $item['id'])
			);
		}
	
		return json_encode($ouput);
	}

}